﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq.Expressions;
using System.Threading.Tasks;
using DailyDiet.Models;
using DailyDiet.Models.VO;
using DailyDiet.Services;
using DailyDiet.Services.Navigation;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;

namespace DailyDiet.ViewModels
{
    /// <summary>
    /// 全部菜品的ViewModel
    /// </summary>
    public class AllDietsPageViewModel : ViewModelBase
    {

        /******** 构造函数 ********/

        /// <summary>
        /// 菜品存储service
        /// </summary>
        private IDietStorageService _dietStorageService;

        /// <summary>
        /// 偏好存储service
        /// </summary>
        private IPreferenceStorage _preferenceStorage;

        private bool _isNew = true;

        /// <summary>
        /// 供演示使用的收藏存储
        /// </summary>
        private IFavoriteStorageService _favoriteStorageService;

        private bool _isLoaded = false;

        /// <summary>
        /// 导航服务
        /// </summary>
        private IContentNavigationService _navigationService;

        private IDietToDietAndPictureService _dietToDietAndPictureService;

        /******** 绑定属性 ********/

        /// <summary>
        /// 菜品集合
        /// </summary>
        public List<Diet> DietsList
        {

            get => _dietsList;
            set => Set(nameof(DietsList), ref _dietsList, value);
        }

        private List<Diet> _dietsList;

        private ObservableCollection<DietAndPicture> _dietAndPictureList =
            new ObservableCollection<DietAndPicture>();
        public ObservableCollection<DietAndPicture> DietAndPictureList {
            get => _dietAndPictureList;
            set =>
                Set(nameof(DietAndPictureList), ref _dietAndPictureList, value);
        }

        /******** 绑定命令 ********/

        /// <summary>
        /// 展示全部菜品命令
        /// </summary>
        private RelayCommand _showAllDietsCommand;
        public RelayCommand ShowAllDietsCommand =>
            _showAllDietsCommand ?? (_showAllDietsCommand = new RelayCommand(async () => await ShowAllDietsCommandFunc()));

        public async Task ShowAllDietsCommandFunc()
        {
            if (!_isNew) {
                return;
            }

            _isNew = false;

            if (_isLoaded) {
                return;
            }
            
            DietsList = await _dietStorageService.GetAllDietsAsync(
                Expression.Lambda<Func<Diet, bool>>(Expression.Constant(true),
                    Expression.Parameter(typeof(Diet), "p")));

            foreach (var diet in DietsList) {
                _dietAndPictureList.Add(await _dietToDietAndPictureService.TranslateOneAsync(diet));
            }

            _isLoaded = true;
            /*DietAndPictureList = new ObservableCollection<DietAndPicture>(
                await _dietToDietAndPictureService
                    .TranslateListAsync(DietsList));*/
        }


        /// <summary>
        /// 处理点击事件命令
        /// </summary>
        private RelayCommand<Diet> _dietTappedCommand;
        public RelayCommand<Diet> DietTappedCommand =>
            _dietTappedCommand ?? (_dietTappedCommand =
                new RelayCommand<Diet>(async diet => await DietTappedCommandFunc(diet)));
        private async Task DietTappedCommandFunc(Diet diet) =>
            await _navigationService.NavigateToAsync(ContentNavigationServiceConstants.DietsDetailPage, diet);


        public AllDietsPageViewModel(
            IDietStorageService dietStorageService, 
            IPreferenceStorage preferenceStorage, 
            IContentNavigationService navigationService, 
            IFavoriteStorageService favoriteStorageService,
            IDietToDietAndPictureService dietToDietAndPictureService)
        {
            _dietStorageService = dietStorageService;
            _preferenceStorage = preferenceStorage;
            _navigationService = navigationService;
            _favoriteStorageService = favoriteStorageService;
            _dietToDietAndPictureService = dietToDietAndPictureService;

            //用于测试初始化DietAndPictureList
            DietAndPictureList = new ObservableCollection<DietAndPicture>();
        }

    }
}
